﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;   

namespace Matopeli
{
    public class Matonen : INotifyPropertyChanged
    {
        #region Constructors

        public Matonen() { }

        public Matonen(double x, double y)
        {
            X = x;
            Y = y;
            Size = 10;
            Length = 0;
        }
 
        #endregion

        #region Properties

        double x;
        public double X
        {
            get { return x; }
            set
            {
                SetField(ref x, value, "X");
                NotifyChange("ActualX");
            }
        }

        double y;
        public double Y
        {
            get { return y; }
            set
            {
                SetField(ref y, value, "Y");
                NotifyChange("ActualY");
            }
        }

        double size;
        public double Size
        {
            get { return size; }
            set
            {
                SetField(ref size, value, "Size");
                NotifyChange("ActualX");
                NotifyChange("ActualY");
            }
        }

        public double ActualX
        {
            get { return X - Size / 2.0; }
        }

        public double ActualY
        {
            get { return Y - Size / 2.0; }
        }

        int length = 0;
        public int Length
        {
            get { return length; }
            set
            {
                SetField(ref length, value, "Length");
            }
        }

        Queue<Point> tail = new Queue<Point>();
        public IEnumerable<Point> Tail
        {
            get { return tail.AsEnumerable(); }
        }

        #endregion

        #region Methods

        public bool Move(double x, double y)
        {
            X = this.x + x;
            Y = this.y + y;

            foreach (var t in tail)
            {
                var dx = X - t.X;
                var dy = Y - t.Y;

                if (Math.Sqrt(dx * dx + dy * dy) < this.size)
                    return false;
            }

            tail.Enqueue(new Point(X, Y));
            if (tail.Count > length)
                tail.Dequeue();

            return true;
        }

        public bool Eat(Omena omena)
        {
            var dx = this.x - omena.X;
            var dy = this.y - omena.Y;

            if (Math.Sqrt(dx * dx + dy * dy) < this.size / 2D + omena.Size / 2D)
            {
                length += 3;
                return true;
            }

            return false;
        }

        #endregion

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        public void SetField<T>(ref T field, T value, string propertyName)
        {
            if (EqualityComparer<T>.Default.Equals(field, value))
                return;

            field = value;

            NotifyChange(propertyName);
        }

        public void NotifyChange(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
